<!DOCTYPE html>
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css"/><!-- bootstrap -->
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script> <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <script type="text/javascript" src="/layer/layer.js"></script><!-- layer -->
    <script type="text/javascript" src="/js/md5.min.js"></script><!-- md5.js -->
    <script type="text/javascript" src="/js/common.js"></script><!-- common.js -->
    <script type="text/javascript" src="/js/socket.js"></script><!-- common.js -->
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
        <div id="userTip" style="display: none">
            <span> 您还没有登录，请<a href="/login.html">登陆</a>后再操作<br/></span>
        </div>
        <span>没有收货地址的提示。。。</span>
    </div>
    <table class="table">
        <tr>
            <td>商品名称</td>
            <td colspan="3" id="goodName"></td>
        </tr>
        <tr>
            <td>商品图片</td>
            <td colspan="3"><img id="goodImg" width="200" height="200"/></td>
        </tr>
        <tr>
            <td>秒杀开始时间</td>
            <td id="startDate"></td>
            <td id="seckillTip">

            </td>
            <td>
                <img id="verifyCodeImg" width="80" height="32" onclick="initVerifyCodeImg()" style="display: none">
                <input id="verifyCode" style="display: none">
                <button class="btn btn-primary btn-block" type="button" display id="buyButton" onclick="getPath()">立即秒杀</button>
            </td>
        </tr>
        <tr>
            <td>商品原价</td>
            <td colspan="3" id="goodPrice"></td>
        </tr>
        <tr>
            <td>秒杀价</td>
            <td colspan="3" id="seckillPrice"></td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td colspan="3" id="stockCount"></td>
        </tr>
    </table>
</div>
<script>
    var seckillId = getQueryString("seckillId")
    $.ajax({
        url: "http://localhost:9000/seckill/seckillGood/queryById?seckillId=" + seckillId,
        type: "get",
        xhrFields: {withCredentials: true},//让浏览器cookie起作用
        success: function (data) {
            if (data.code == 200) {
                //{200，操作成功，vo}
                renderData(data.data)
                renderDate(data.data.startDate, data.data.endDate);
            } else {
                layer.msg("错误码：" + data.code + "错误信息:" + data.msg)
            }
        }

    })

    $.ajax({
        url: "http://localhost:9000/member/getCurrent",
        type: "get",
        xhrFields: {withCredentials: true},//让浏览器cookie起作用
        success: function (data) {
            if (data.code == 200) {
                //{200，操作成功，vo}
                showUserStatus(data.data)
            } else {
                layer.msg("错误码：" + data.code + "错误信息:" + data.msg)
            }
        }
    })
    var u;
    var uuid;
    function showUserStatus(user) {
        //user没值，展示需要登录
        if (!user) {
            $("#userTip").show()
            return
        }
        u=user;
        uuid=getUuid(user.id)
        //登录成功展示验证码
        $("#verifyCode").show()
        $("#verifyCodeImg").show()
        initVerifyCodeImg()
    }
    function initVerifyCodeImg(){
        $("#verifyCodeImg").prop("src","http://localhost:9000/seckill/initVerifyCode?uuid="+uuid+"&time="+new Date().getTime());
    }
    function getPath() {
        if (!u){
            layer.msg("请登录，再操作！")
            return;
        }
        $.ajax({
            url: "http://localhost:9000/seckill/getPath",
            type: "post",
            data:{"uuid":uuid},
            xhrFields: {withCredentials: true},//让浏览器cookie起作用
            success: function (data) {
                if (data.code == 200) {
                doSeckill(data.data)
                } else {
                    layer.msg("错误码：" + data.code + "错误信息:" + data.msg)
                }
            }
        })


    }
    function doSeckill(path) {
        if (!u){
            layer.msg("请登录，再操作！")
            return;
        }
        $.ajax({
            url: "http://localhost:9000/seckill/"+path+"/doSeckill",
            type: "post",
            data:{"seckillId":seckillId,"uuid":uuid,"verifyCode":$("#verifyCode").val()},
            xhrFields: {withCredentials: true},//让浏览器cookie起作用
            success: function (data) {
                if (data.code == 200) {
                    //{200，操作成功，orderno};成功返回订单编号
                  // window.location.href="/order_detail.html?orderNo="+data.data
                    layer.msg(data.data)
                    //建立联系
                    createScoket(uuid);
                } else {
                    layer.msg("错误码：" + data.code + "错误信息:" + data.msg)
                }
            }
        })

    }

    function renderData(data) {
        console.log(data)
        $("#goodName").html(data.goodName);
        $("#goodImg").prop("src", data.goodImg);
        $("#startDate").html(data.startDate);
        $("#goodPrice").html(data.goodPrice);
        $("#seckillPrice").html(data.seckillPrice);
        $("#stockCount").html(data.stockCount);
    }

    var remainStartSeconds;   //距离开始有多长时间
    var remainEndSeconds;    //距离结束有多长时间
    var timer;

    function renderDate(startDate, endDate) {
        /**
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   11 : 30
         * startDate - now > 0      ======>距离秒杀开始还有多长时间
         * 秒杀状态 : 倒计时
         *
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   12:30
         * endDate = 2020 -4 - 24  13:00
         * startDate -  now  < 0
         * endDate - now > 0        ========> 距离秒杀结束还有多长时间
         * 秒杀状态  : 秒杀中
         *
         *
         * startDate = 2020 -4 - 24  12:00
         * now  = 2020 - 4 - 24   13:30
         * endDate = 2020 -4 - 24  13:00
         * startDate - now < 0
         * endDate - now < 0
         * 秒杀状态 : 秒杀已经结束了
         *
         */
        var startDate = new Date(startDate);
        var endDate = new Date(endDate);
        var now = new Date();
        remainStartSeconds = parseInt((startDate.getTime() - now.getTime()) / 1000);
        remainEndSeconds = parseInt((endDate.getTime() - now.getTime()) / 1000);

        timer = setInterval(showStatus, 1000);
    }

    function showStatus() {
        remainStartSeconds--;
        remainEndSeconds--
        if (remainStartSeconds > 0) {
            //秒杀还未开始
            $("#seckillTip").html("距离秒杀开始还有" + remainStartSeconds + " 秒")
        } else if (remainEndSeconds > 0) {
            //秒杀中
            $("#seckillTip").html("距离秒杀结束还有" + remainEndSeconds + " 秒")
             $("#buyButton").prop("disabled",false);
        } else {
            //秒杀结束
            $("#seckillTip").html("秒杀结束");
            //秒杀按钮灰色
             $("#buyButton").prop("disabled",true);

            clearInterval(timer);
        }
    }
</script>
</body>
</html>